﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Frogger
{
    class Frog : MoveableEntity
    {
        Cue __Cue;
        int __waitCount = 0;

        //Frog states
        public enum FrogState { Stop,Jump, Dead };
        public FrogState __currentFrogState = FrogState.Stop;

        public bool isJumping()
        {
            if (__currentFrogState == FrogState.Jump)
            {
                return true;
            }
            else
                return false;
        }

        public float Scale
        {
            get
            {
                return this.__scale;
            }

            protected set
            {
                this.__scale = value;
            }
        }

        public Frog(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, float scale, Cue cue)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed, null, 0, scale) //If frog has Music change null to music name_Iric
        {
            this.__Cue = cue;
        }

        public Frog(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, int millisecondsPerFrame, Cue cue)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed, millisecondsPerFrame, null, 0)
        {
            this.__Cue = cue;
        }

        public override Vector2 direction
        {
            get
            {
                //Return direction based on input from mouse and gamepad
                Vector2 inputDirection = Vector2.Zero;
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    inputDirection.X -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    inputDirection.X += 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    inputDirection.Y -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    inputDirection.Y += 1;

                return inputDirection * __speed;
            }
            protected set
            {
                this.__speed = value;
            }
        }

        //Return the collision Rectangle
        public override Rectangle collisionRect
        {
            get
            {
                return new Rectangle(
                    (int)(__position.X + (__collisionOffset * __scale)),
                    (int)(__position.Y + ( 7.5 * __collisionOffset * __scale)),
                    (int)((__frameSize.X - (__collisionOffset * 2)) * __scale),
                    (int)((__frameSize.Y - (__collisionOffset * 2 * 1.5)) * __scale));
            }
        }

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            switch ( __currentFrogState )
            {
                case FrogState.Stop:
                    {
                        __waitCount++;
                        if (__waitCount > Constants.WAIT_TIME)
                        {
                            __waitCount = 0;
                            this.__currentFrogState = FrogState.Jump;
                            __Cue.Play();
                        }
                        break;
                    }
                case FrogState.Jump:
                    {
                        __currentFrame.Y++;

                        if(__position.Y< (clientBounds.Height/3) * 2)
                            __position = __position + __speed;

                        if (__currentFrame.Y >= __sheetSize.Y)
                        {
                            __currentFrame.Y = 0;
                            __currentFrogState = FrogState.Stop;
                            __Cue.Stop();
                        }
                        break;
                    }
                case FrogState.Dead:
                    {
                        break;
                    }
            }

            base.Update(gameTime, clientBounds);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }
    }
}
